<?php
namespace app\admin\controller;

use think\Cookie;
use think\Cache;
use think\Db;

class Admin extends Common
{
    public function index()
    {	
    	//获取model中admin.php中的getAllData() 返回的值
    	$data = model("Admin")->getAllData(1);

        // $list = Db::name('admin')->select();
        $this->assign('data',$data);	
        return $this->fetch();

    }
    //软删除
    public function del(){
    	//获取的是一个数组
    	$ids = input('post.ids/a');
    	//将数组转为字符串 以逗号分隔
    	$ids = implode(',',$ids);
    	//软删除 更新状态
    	$result = Db::name('admin')->where("id in ($ids)")->setField('status',0);
    	if($result){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }
    //修改on_off
    public function off(){
    	//获取id
    	$id = input("post.id");
    	// dump($id);die;
    	$result = Db::name('admin')->where('id',$id)->setField('on_off',0);

    	if($result){
    		// $this->success("更新成功");
    		return json(array('code'=>1,'msg'=>"停用成功"));
    	}else{
    		// $this->error("更新失败");
    		return json(array('code'=>0,'msg'=>"停用失败"));
    	}
    }
     public function on(){
    	//获取id
    	$id = input("post.id");
    	// dump($id);die;
    	$result = Db::name('admin')->where("id",$id)->setField('on_off',1);

    	if($result){
    		// $this->success("更新成功");
    		return json(array('code'=>1,'msg'=>"开启成功"));
    	}else{
    		// $this->error("更新失败");
    		return json(array('code'=>0,'msg'=>"开启失败"));
    	}
    }

    //添加管理员
    public function add(){
    	if(request()->isPost()){
                // $id = Cookie::get('id');
                // Cache::rm('user_'.$id);

	    		$data = input('post.data/a');
	    		// return $data;
	    		
	    		if(!$data){
	    			return json(array('code'=>0,'msg'=>"参数错误"));
	    		}
				
	    		$admin_id = model('admin')->addAdmin($data);
	    		
	    		
	    		if (!$admin_id) {
	    			return json(array('code'=>0,'msg'=>"添加失败"));
	    		}
	    		//把role_id写入admin_role表
	    		$role_id = implode(',',$data['role_id']);
	    		
	    		// 将两个数据整合成成一个数组
	    		$data2 = ['admin_id'=>$admin_id,'role_id'=>$role_id];
	    		$result = Db::name('admin_role')->insert($data2);
	    		if($result){
	    			return json(['code'=>1,'msg'=>'添加成功']);
	    		}else{
	    			return json(['code'=>0,'msg'=>'添加失败']);
	    		}

    	}else{
				$data = Db::name('role')->where('status',1)->field('id,role_name')->select();
		    	$this->assign('data',$data);
		    	return $this->fetch();
    	}
    	
    }

    public function edit($id=''){
    	
    	if(request()->isPost()){
    		//获取输入框传过来的值
    		$data = input('post.data/a');
    		// dump($data);die;
    		 
			//将获取的角色id转成字符串 
    		$role_id = implode(',',$data['role_id']);

    		 $result1 = Db::name('admin')->where('id',$data['id'])->update(['username'=>$data['username'],'password'=>$data['password']]);
    		 $result2 = Db::name('admin_role')->where('admin_id',$data['id'])->update(['role_id'=>$role_id]);
    		 if ($result1 && $result2) {
    		 		return json(['code'=>1,'msg'=>'修改成功']);
    		 }else if($result1 or $result2){
	    			return json(['code'=>1,'msg'=>'修改成功']);
	    	 }else{
	    			return json(['code'=>0,'msg'=>'修改失败']);
	    	 }
    		 
    		 
    	}else{
    		// dump($id);
	    	$data = Db::name('role')->field('id,role_name')->select();
	    	$this->assign('data',$data);

	    	$admin = model('admin')->editAdmin($id);
	    	// dump($admin);die;
			$this->assign('admin',$admin);
	    	return $this->fetch();
    	}
    	
    }
    //导入教师或教师管理员信息
    public function import(){
       action('admin/Excel/impUser',['table_name'=>'em_admin']);
    }    
}
